<template>
  <el-card class="template-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">导入数据表</el-button>

      </div>
    </template>
    <el-table v-loading="loading" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="tableName" label="table">
      </el-table-column>
      <el-table-column prop="tableComment" label="Comment" >
      </el-table-column>

      <el-table-column prop="className" label="类名" >
      </el-table-column>

      <el-table-column label="操作" width="220">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改表信息</a>
  
          <el-popconfirm title="确定删除吗？" @confirm="handleDeleteOne(scope.row.id)">
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize"
      :current-page="currentPage" @current-change="changePage" />
    <DialogTableImport ref='addDb' :reload="getDataList" :type="type" />
    <DialogTableInfo ref='tbInfo' :reload="getDataList" :type="type" />
  </el-card>
</template>

<script lang="ts">
  import DialogTableImport from '@/components/DialogTableImport.vue'
  import DialogTableInfo from '@/components/DialogTableInfo.vue'
  import {
    onMounted,
    onUnmounted,
    reactive,
    ref,
    toRefs
  } from 'vue'
  import * as db_context from '@/models/db_context'




  export default {
    name: 'DB',
    components: {
      DialogTableImport,
      DialogTableInfo
    },
    setup() {

      const addDb = ref < any > (null)
      const tbInfo = ref < any > (null)
      const multipleTable = ref(null)

      const state = reactive({
        loading: false,
        tableData: [] as any[], // 数据列表
        multipleSelection: [], // 选中项
        total: 0, // 总条数
        currentPage: 1, // 当前页
        pageSize: 10, // 分页大小
        type: 'add', // 操作类型
        level: 1,
        parent_id: 0
      })

      onMounted(() => {
        getDataList()
      })

      // 获取数据列表
      const getDataList = () => {
        db_context.GenTable
          .getList < db_context.GenTable > ()
          .then(results => {
            console.log('resultsxxxxx:', results);
            state.tableData = results;
          });
      }

      // 添加分类
      const handleAdd = () => {
        state.type = 'add' // 传入弹窗组件用于弹窗 title 判断
        addDb.value.open()
      }
      // 修改分类
      const handleEdit = (id: string) => {
        state.type = 'edit' // 传入弹窗组件用于弹窗 title 判断
        tbInfo.value.open(id)
      }
      //预览代码
  const handleGenCode = (id: string) => {
        state.type = 'edit' // 传入弹窗组件用于弹窗 title 判断
        tbInfo.value.open(id)
      }
      


      // 选择项
      const handleSelectionChange = () => {
        console.log('handleSelectionChange');
      }
      // 批量删除
      const handleDelete = (v: string) => {
        console.log('handleDelete----' + v);

      }

      const toHump = ((str: string): string => {

        var arr = str.split('_');
        return arr.map(function (item, index) {
          if (index === 0) {
            return item;
          } else {
            return item.charAt(0).toUpperCase() + item.slice(1);
          }
        }).join('');


      })

      // 单个删除
      const handleDeleteOne = async (v: string) => {
        console.log('handleDelete----' + toHump("aaa_bbb_ccc") + v);

        await db_context.GenTable.deleteById(Number.parseInt(v));
        getDataList();

      }

      return {
        ...toRefs(state),
        multipleTable,
        handleAdd,
        handleEdit,
        handleSelectionChange,
        handleDelete,
        handleDeleteOne,
        getDataList,
        addDb,
        tbInfo,
        handleGenCode


      }
    }
  }
</script>